// pages/bizcount/bizcount.js

const app = getApp()
import http from "../../utils/http.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countList: null,
    start: "请选择市场",
    selList:[],
    orgCode:'',
    isstart: false,
    allHeight:0,
    isShowSel:false,
    orgCode: '',
    codeIndex: 0,
    selListName:[],
    selIndex:0,
  },
  opens: function(e) {
    switch (e.currentTarget.dataset.item) {
      case "1":
        if (this.data.isstart) {
          this.setData({
            isstart: false,
          });
        } else {
          this.setData({
            isstart: true,
          });
        }
        break;
    }
  },
  // selItem: function(e) {
  //   let index = e.currentTarget.dataset.index;
  //   let orgCode = e.currentTarget.dataset.code;
  //   let name = this.data.selList[index].name;
  //   this.setData({
  //     isstart: false,
  //     start: this.data.selList[index].name,
  //     orgCode: orgCode,
  //   })
  //   this.getdata();
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  getdata:function(){
    let that = this;
    let orgCode = this.data.orgCode
    http.request({
      url: '/applySummary/findApplySummary',
      data: {
        orgCode: orgCode
      },
      success: (result) => {
        that.setData({
          countList: result
        })

        // 设置menu高度
        let query = wx.createSelectorQuery()
        query.select('#listData').boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec((res) => {
          this.listHeight = res[0].height
          this.listTop = res[0].top
        })
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              allHeight: res.windowHeight-230
            })
          }
        })
      }
    });
  },
  onLoad: function(options) {
    let that = this;
    this.getdata();
    http.request({
      url: '/auth/findOrgTree',
      data: {
        orgType:4
      },
      success: (result) => {
        let selData = result;
        let isShowSel = this.data.isShowSel;
        if (result.length >1){
          isShowSel = true;
        }else{
          isShowSel = false;
        }
        const all = {
          name:'全部',
          code:'',
        }
        selData.unshift(all);
        that.setData({
          isShowSel:isShowSel,
          selList: selData
        })
        let selListName = [];
        for(let i =0;i<result.length;i++){
          selListName.push(result[i].name);
        }
        this.setData({
          selListName:selListName
        })
      }
    });
  },
  //市场选择
  bindOrgCode: function (e) {
    let idx = e.detail.value;
    let codeData = this.data.codeData;
    let orgCode = this.data.orgCode;
    let selList = this.data.selList;
    let selListName = this.data.selListName;
    orgCode = selList[idx].code
    this.setData({
      codeIndex: idx,
      orgCode:orgCode
    })
    this.getdata();
  },
})